window.onload = function () {
    getGoodList();
    getDiscountList();
}


//getGoodList 获取商品列表数据
function getGoodList() {
    $.get("/goodListGET", function (res) {
        let tableList = document.getElementsByClassName("goods-warp")
        tableList[0].innerHTML += addGoodListToHTML(res.data)
    })
}

//getGoodList 获取
function getDiscountList() {
    $.get("/discountListGET", function (res) {
        window.discountList = res.data.discountList;
    })
}

//addGoodListToHTML 给表格添加数据
function addGoodListToHTML(data) {
    let s = '<tbody>'
    window.goodPrice = [];
    for (let i = 0; i < data.length; ++i) {
        goodPrice.push(data[i].GoodPrice);
        s += '' +
            '<tr>' +
            '<th scope="row" class="goodNo">' + data[i].GoodNo + '</th>' +
            '<td>' + data[i].GoodName.String + '</td>' +
            '<td>' + data[i].GoodPrice + '</td>' +
            '<td><input type="text" oninput="onInputGoodTotalPrice()" class="requireNum" value="0"></td>' +
            '<td><div class="totalPrice">0</div></td>' +
            '<td><input type="date" class="deliveryDate " value="2021-05-05"/></td>' +
            '</tr>';
    }
    s += '</tbody>';

    return s;
}

function onInputGoodTotalPrice() {
    //添加监听请求数量事件
    console.log("change");
    let requireNum = document.getElementsByClassName("requireNum");
    let goodTotalPrice = document.getElementsByClassName("totalPrice");
    let totalPrice=0;
    let discount=0;
    for (let i = 0; i < requireNum.length; i++) {
        goodTotalPrice[i].innerHTML = String(Number(requireNum[i].value) * Number(goodPrice[i]))
        totalPrice += Number(requireNum[i].value) * Number(goodPrice[i]);
    }
    for (let i = 0; i < discountList.length; i++) {
        if (totalPrice >= discountList[i].MinMoney && totalPrice <= discountList[i].MaxMoney) {
            discount = discountList[i].DiscountRate;
        }
    }
    console.log(totalPrice + "-" + discount + "-" + totalPrice * discount)
    $("#totalPrice").text(totalPrice);
    $("#discount").text(discount);
    $("#totalPriceReal").text(totalPrice * discount);
}

//postIndent 提交订单
function postIndent() {
    let l = createIndentList();
    let date = new Date();
    $.post("/operateCenter/submitOrder", {
        goodNoList: l.goodNoList.toString(),
        requireNumList: l.requireNumList.toString(),
        deliveryDateList: l.deliveryDateList.toString()
    }, function (res, status) {
        if (res.code === "IndentOK") {
            alert("下达订单成功，跳转至操作中心--->")
            window.location = "../operateCenter"
        }
    })
}

//createIndentList 生成订单
function createIndentList() {
    //首先生成各个组件的对象
    let goodNo = document.getElementsByClassName("goodNo");
    let requireNum = document.getElementsByClassName("requireNum");
    let deliveryDate = document.getElementsByClassName("deliveryDate");
    let goodNoList = [];
    let requireNumList = [];
    let deliveryDateList = [];
    for (let i = 0; i < goodNo.length; ++i) {
        //如果需求个数为0个就不需要添加
        if (requireNum[i].value !== "0") {
            goodNoList.push(goodNo[i].innerHTML);
            requireNumList.push(requireNum[i].value);
            deliveryDateList.push(deliveryDate[i].value);
        }
    }
    return {
        goodNoList: goodNoList,
        requireNumList: requireNumList,
        deliveryDateList: deliveryDateList
    }
}
